package com.dfl.gankio.ui.main

import android.util.Log
import androidx.core.view.GravityCompat
import androidx.appcompat.app.ActionBarDrawerToggle
import android.view.MenuItem
import androidx.drawerlayout.widget.DrawerLayout
import com.google.android.material.navigation.NavigationView
import androidx.appcompat.widget.Toolbar
import android.view.View
import com.dfl.gankio.R
import com.dfl.gankio.base.BaseFragment
import com.dfl.gankio.bean.BannerBean
import com.dfl.gankio.net.NetObserver
import kotlinx.android.synthetic.main.fragment_main.*

class MainFragment : BaseFragment<MainViewModel>(),
    NavigationView.OnNavigationItemSelectedListener {

    override fun getViewModelClass(): Class<MainViewModel> {
        return MainViewModel::class.java
    }

    override fun getLayoutId(): Int {
        return R.layout.fragment_main
    }

    override fun initView(view: View) {
        val toolbar: Toolbar = view.findViewById(R.id.toolbar)
        val drawerLayout: DrawerLayout = view.findViewById(R.id.drawer_layout)
        val navView: NavigationView = view.findViewById(R.id.nav_view)
        val toggle = ActionBarDrawerToggle(
            activity,
            drawerLayout,
            toolbar,
            R.string.navigation_drawer_open,
            R.string.navigation_drawer_close
        )
        drawerLayout.addDrawerListener(toggle)
        toggle.syncState()
        navView.setNavigationItemSelectedListener(this)
        toolbar.setTitle(R.string.app_name)
    }


    override fun onBackPressed() {
        val drawerLayout: DrawerLayout = drawer_layout
        if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
            drawerLayout.closeDrawer(GravityCompat.START)
        } else {
            super.onBackPressed()
        }
    }

    override fun onNavigationItemSelected(item: MenuItem): Boolean {
        // Handle navigation view item clicks here.
        when (item.itemId) {
            R.id.nav_home -> {

            }
            R.id.nav_gallery -> {

            }
            R.id.nav_slideshow -> {

            }
            R.id.nav_tools -> {

            }
        }
        val drawerLayout: DrawerLayout = drawer_layout
        drawerLayout.closeDrawer(GravityCompat.START)
        return true
    }

    override fun initData() {
        viewModel.getNewData()
            .observe(this, object : NetObserver<List<BannerBean>>() {

                override fun onError(emssage: String) {

                }

                override fun onSuccess(data: List<BannerBean>) {
                    Log.d("MainFragment", "" + data.size)
                }
            })
    }
}
